﻿using Blm.Utils.Extensions;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using VisionCloud.Domain.Common;
using VisionCloud.Domain.DtoModel.Sys;
using VisionCloud.Domain.Filters;
using VisionCloud.Service.Sys;

namespace VisionCloud.Api.Controllers.Sys
{
    /// <summary>
    /// 用户角色控制器
    /// </summary>
    [SystemModule("用户角色控制器")]
    [ApiAuthorize(PolicyEnum.RequireRolePermission)]
    [ApiVersion("1.0")]
    [Route("api/sys/[controller]/[Action]")]
    [ApiExplorerSettings(GroupName = "用户角色控制器")]
    public class UserRoleController : ApiBaseController
    {
        /// <summary>
        /// 用户角色服务
        /// </summary>
        public UserRoleService UserRoleService { get; set; }

        #region 角色

        /// <summary>
        /// 创建用户角色
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> CreateUserRole(SysUserRoleDto model)
        {
            return await TryCatchAsync(UserRoleService.CreateUserRole(model, TokenModel.YunUserGuid));
        }

        /// <summary>
        /// 编辑用户角色
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> EditUserRole(SysUserRoleDto model)
        {
            return await TryCatchAsync(UserRoleService.UpdateUserRole(model, TokenModel.YunUserGuid));
        }

        /// <summary>
        /// 删除用户角色
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> DeleteUserRole(SysUserRoleDto model)
        {
            return await TryCatchAsync(UserRoleService.DeleteUserRole(model, TokenModel.YunUserGuid));
        }

        /// <summary>
        /// 交换用户角色
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> TransferUserRole(UserRoleTransferDto model)
        {
            if (model.FromUserId == Guid.Empty)
            {
                model.FromUserId = TokenModel.YunUserGuid;
                model.FromRoleId = TokenModel.Role.ToGuid();
            }
            return await TryCatchAsync(UserRoleService.TransferUserRole(model, TokenModel.YunUserGuid));
        }

        /// <summary>
        /// 获取用户角色列表
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetUserRoleList(SysUserRoleCondition condition)
        {
            return await TryCatchAsync(UserRoleService.GetPage(condition));
        }

        #endregion 角色
    }
}